package com.googlecode.openauth.web.util;

import static org.junit.Assert.*;

import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.googlecode.openauth.web.entity.ActionPageBean;
import com.googlecode.openauth.web.entity.UserInfo;

public class UtilTest {
	
	private static Logger logger = Logger.getLogger(UtilTest.class);

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
	}

	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testCalcNextRowBounds() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(0, rowBounds.getOffset());
		assertEquals(1, pageBean.getCurrentPageNo());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(1, pageBean.getNextPageNo());
	}

	@Test
	public void testCalcNextRowBounds1() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(1);
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(0, rowBounds.getOffset());
		assertEquals(1, pageBean.getCurrentPageNo());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(1, pageBean.getNextPageNo());
	}
	
	
	@Test
	public void testCalcNextRowBounds2() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(2);
		pageBean.setCurrentPageNo(1);
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(1, rowBounds.getOffset());
		assertEquals(2, pageBean.getCurrentPageNo());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(2, pageBean.getNextPageNo());
	}
	
	@Test
	public void testCalcNextRowBounds3() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(2);
		pageBean.setCurrentPageNo(2);
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(1, rowBounds.getOffset());
		assertEquals(2, pageBean.getCurrentPageNo());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(2, pageBean.getNextPageNo());
	}
	
	@Test
	public void testCalcNextRowBounds4() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(2);
		pageBean.setCurrentPageNo(3);
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(1, rowBounds.getOffset());
		assertEquals(2, pageBean.getCurrentPageNo());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(2, pageBean.getNextPageNo());
	}
	
	@Test
	public void testCalcNextRowBounds5() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(2);
		pageBean.setCurrentPageNo(1);
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(1, rowBounds.getOffset());
		assertEquals(2, pageBean.getCurrentPageNo());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(2, pageBean.getNextPageNo());
		logger.info(pageBean);
	}
	
	@Test
	public void testCalcNextRowBounds6() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(5);
		pageBean.setPageNum(3);
		pageBean.setCurrentPageNo(1);
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		assertEquals(3, rowBounds.getLimit());
		assertEquals(1, rowBounds.getOffset());
		assertEquals(2, pageBean.getCurrentPageNo());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(2, pageBean.getNextPageNo());
		logger.info(pageBean);
	}
	
	@Test
	public void testCalcNextRowBounds7() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(8);
		pageBean.setPageNum(3);
		pageBean.setCurrentPageNo(1);
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		assertEquals(3, rowBounds.getLimit());
		assertEquals(1, rowBounds.getOffset());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(2, pageBean.getCurrentPageNo());
		assertEquals(3, pageBean.getNextPageNo());
		logger.info(pageBean);
	}
	
	@Test
	public void testCalcNextRowBounds8() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(8);
		pageBean.setPageNum(1);
		pageBean.setCurrentPageNo(6);
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(6, rowBounds.getOffset());
		assertEquals(6, pageBean.getPrevPageNo());
		assertEquals(7, pageBean.getCurrentPageNo());
		assertEquals(8, pageBean.getNextPageNo());
		logger.info(pageBean);
	}
	
	@Test
	public void testGetMaxPageNo(){
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(8);
		pageBean.setPageNum(3);
		int maxPageNo = util.getMaxPageNo(pageBean);
		assertEquals(1, pageBean.getCurrentPageNo());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(1, pageBean.getNextPageNo());
		assertEquals(3, maxPageNo);
	}
	
	@Test
	public void testGetMaxPageNo1(){
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(10);
		pageBean.setPageNum(5);
		int maxPageNo = util.getMaxPageNo(pageBean);
		assertEquals(1, pageBean.getCurrentPageNo());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(1, pageBean.getNextPageNo());
		assertEquals(2, maxPageNo);
	}
	
	@Test
	public void testCalcPrevRowBounds1() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(2);
		pageBean.setPageNum(1);
		pageBean.setCurrentPageNo(1);
		RowBounds rowBounds = util.calcPrevRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(0, rowBounds.getOffset());
		assertEquals(1, pageBean.getCurrentPageNo());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(2, pageBean.getNextPageNo());
		logger.info(pageBean);
	}
	
	@Test
	public void testCalcPrevRowBounds2() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(3);
		pageBean.setPageNum(1);
		pageBean.setCurrentPageNo(2);
		RowBounds rowBounds = util.calcPrevRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(0, rowBounds.getOffset());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(1, pageBean.getCurrentPageNo());
		assertEquals(2, pageBean.getNextPageNo());
		logger.info(pageBean);
	}
	
	@Test
	public void testCalcPrevRowBounds3() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(4);
		pageBean.setPageNum(1);
		pageBean.setCurrentPageNo(3);
		RowBounds rowBounds = util.calcPrevRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(1, rowBounds.getOffset());
		assertEquals(1, pageBean.getPrevPageNo());
		assertEquals(2, pageBean.getCurrentPageNo());
		assertEquals(3, pageBean.getNextPageNo());
		logger.info(pageBean);
	}
	
	@Test
	public void testCalcPrevRowBounds4() {
		Util util = new Util();
		ActionPageBean<UserInfo> pageBean = new ActionPageBean<UserInfo>();
		pageBean.setTotalCount(5);
		pageBean.setPageNum(1);
		pageBean.setCurrentPageNo(4);
		RowBounds rowBounds = util.calcPrevRowBounds(pageBean);
		assertEquals(1, rowBounds.getLimit());
		assertEquals(2, rowBounds.getOffset());
		assertEquals(2, pageBean.getPrevPageNo());
		assertEquals(3, pageBean.getCurrentPageNo());
		assertEquals(4, pageBean.getNextPageNo());
		logger.info(pageBean);
	}
}
